import 'package:floor/floor.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/models/base_model.dart';
import 'package:share_fit/app/tools/date_tool.dart';
import 'package:share_fit/generated/locales.g.dart';

@Entity(tableName: 'sleep', indices: [
  Index(value: ['calendar', 'address'])
])
class SleepModel extends BaseModel {
  @ColumnInfo(name: 'calendar_time')
  late String? calendarTime;
  late String? calendar;
  late int? time;

  /// 开始时间，格林尼治时间，单位：秒
  @ColumnInfo(name: 'start_time')
  late int? startTime;

  /// 结束时间，格林尼治时间，单位：秒
  @ColumnInfo(name: 'end_time')
  late int? endTime;

  /// 睡眠状态， 1：深睡Deep sleep，2:浅睡light sleep，3:清醒Awake，4: 快速眼动REM，5:零星小睡Naps，7:开始时间Start time，8:结束时间End time
  @ColumnInfo(name: 'sleep_type')
  late int? sleepType;

  /// 睡眠时长，单位：分钟
  @ColumnInfo(name: 'sleep_time')
  late int? sleepTime;

  /// 是否为小睡数据， true为是，false为否
  @ColumnInfo(name: 'is_snooze')
  late bool? isSnooze;

  SleepModel({
    super.id,
    super.address,
    super.dateCreated,
    super.dateModified,
    this.calendarTime,
    this.calendar,
    this.time,
    this.startTime,
    this.endTime,
    this.sleepType,
    this.sleepTime,
    this.isSnooze,
  });

  @override
  toMap() {
    return {'calendar': calendar, 'sleepType': sleepType, 'sleepTime': sleepTime};
  }

  get sleepSummary => SleepSummary(sleepTimeSum: sleepTime ??= 0);

  @override
  bool from(Map map) {
    // TODO: implement from
    throw UnimplementedError();
  }

  @override
  List<Object?> get props => [calendarTime, calendar, time, startTime, endTime, sleepType, sleepTime, isSnooze];

  @override
  bool? get stringify => true;

  get dateTooltipText {
    DateTime start = DateTime.fromMillisecondsSinceEpoch(startTime! * 1000);
    DateTime end = DateTime.fromMillisecondsSinceEpoch(endTime! * 1000);
    return '$sleepTypeText\t${DateTool.mdFormat(start)}\n${DateTool.hsFormat(start)}~${DateTool.hsFormat(end)}';
  }

  get napTimeRangeText {
    DateTime st = DateTool.getDateTimeFrom(startTime!);
    DateTime et = DateTool.getDateTimeFrom(endTime!);
    return '${DateTool.hsFormat(st)} - ${DateTool.hsFormat(et)}';
  }

  /// 分钟转为时:分格式
  get formatNapTimeText {
    return DateTool.second2HM(sleepTime! * 60);
  }

  get sleepTypeText {
    if (sleepType == 1) {
      return LocaleKeys.sleep_deep_text.ts;
    } else if (sleepType == 2) {
      return LocaleKeys.sleep_light_text.ts;
    } else if (sleepType == 3) {
      return LocaleKeys.sleep_sober_text.ts;
    } else if (sleepType == 4) {
      return LocaleKeys.sleep_rem_text.ts;
    } else {
      return LocaleKeys.sleep_naps_text.ts;
    }
  }
}

class SleepSummary {
  late int sleepTimeSum;
  late int sleepTarget = 8;

  late String calendar;
  late List<SleepModel> models;

  SleepSummary({this.sleepTimeSum = 0, this.calendar = '', this.models = const []});

  setTarget({int? target}) {
    sleepTarget = target ?? 8;
  }

  get totalSleepTime => models.isEmpty ? 0 : models.map((e) => e.sleepTime).reduce((value, element) => value! + element!);

  /// 获取睡眠总时长
  get sleepSumText {
    if (sleepTimeSum == 0) {
      return LocaleKeys.no_records_text.ts;
    }
    return '$sleepSumH${LocaleKeys.time_unit_hour.ts}$sleepSumM${LocaleKeys.time_unit_minute.ts}';
  }

  get sleepSumM {
    int sum = sleepTimeSum;
    int min = sum % 60;
    return min;
  }

  get sleepSumH {
    int sum = sleepTimeSum;
    int hours = sum ~/ 60;
    return hours;
  }

  get getProgressValue {
    return sleepTarget <= 0 ? 0.0 : double.parse((sleepTimeSum / (sleepTarget * 60)).toStringAsFixed(2));
  }
}
